package com.yoga.controller;


import com.yoga.entity.YogaUser;
import com.yoga.service.LoginService;
import com.yoga.utils.LoginCode;
import com.yoga.utils.ResultVO;
import com.yoga.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {
    @Autowired
    private LoginService userservice;

    @RequestMapping("login")
    public ResultVO login(@RequestBody YogaUser yogaUser, HttpSession session){
        YogaUser yogaUser1 = userservice.login(yogaUser);
        if (yogaUser1!=null){
            session.setAttribute("yogaUserinfo1",yogaUser1);
            Map<String,Object> map = new HashMap<>();
            map.put("yogaUserinfo1",yogaUser1);
            return ResultVO.success("登录成功",map);
        }
        return ResultVO.fail("登录失败");
    }
    @RequestMapping("logins")
    public ResultVO logins(@RequestParam String phone, @RequestParam String captcha, HttpSession session){
        String storedCaptcha = (String) session.getAttribute("captcha");
        if (storedCaptcha != null && storedCaptcha.equals(captcha)) {
            YogaUser yogaUser1 = userservice.logins(phone);
            if (yogaUser1 != null) {
                session.setAttribute("yogaUser1", yogaUser1);
                Map<String, Object> map = new HashMap<>();
                map.put("yogaUser1", yogaUser1);
                return ResultVO.success("登录成功", map);
            }
            return ResultVO.fail("该手机号未注册");
        }else {
            return ResultVO.fail("验证码错误");
        }
    }
    @RequestMapping("updatepwd")
    public ResultVO updatepwd(@RequestParam Integer uid,@RequestParam String upwd){
        boolean flag = userservice.update(uid, upwd);
        return ResultVO.success("修改成功",flag);
    }
    @RequestMapping("addUser")
    public ResultVO addUser(@RequestBody YogaUser yogaUser,@RequestParam String captcha,HttpSession session){
        String storedCaptcha = (String) session.getAttribute("captcha");
        if (storedCaptcha != null && storedCaptcha.equals(captcha)){
            boolean flag = userservice.save(yogaUser);
            return ResultVO.success("添加成功",flag);
        }else {
            return ResultVO.fail("添加失败");
        }

    }
    @RequestMapping("addCAPTCHA")
    public ResultVO addCAPTCHA(@RequestParam String phone,HttpSession session){
        YogaUser yogaUser = userservice.loginCode(phone);
        System.out.println(yogaUser);
        if(yogaUser==null){
            return ResultVO.fail("手机号未注册");
        }
        if (!TimeUtils.canSendCaptcha(session, phone)) {
            return ResultVO.fail("请等待60秒后再次发送验证码");
        }
        String captcha = LoginCode.getLoginCode();
//        boolean b = SendCode.sendCode(phone, captcha);
        session.setAttribute("captcha", captcha);
        TimeUtils.updateLastSendTime(session, phone);
        System.out.println(captcha);
        return ResultVO.success("验证码已发送",null);
    }
    @RequestMapping("checkCountdownStatus")
    public ResultVO checkCountdownStatus(@RequestParam String phone, HttpSession session) {
        Long lastSendTime = (Long) session.getAttribute(phone + "_lastSendTime");
        if (lastSendTime == null) {
            return ResultVO.success("没有倒计时", 0);
        }

        long currentTime = System.currentTimeMillis();
        long elapsedTime = (currentTime - lastSendTime);
        long remainingTime = Math.max(0, (TimeUtils.INTERVAL - elapsedTime) / 1000);

        return ResultVO.success("倒计时状态", remainingTime);
    }
    @RequestMapping("loginCode")
    public ResultVO loginCode(@RequestParam String phone, @RequestParam String captcha, HttpSession session){
//        boolean b = userservice.loginCode(phone);
        System.out.println("Received phone: " + phone + ", captcha: " + captcha);
        String storedCaptcha = (String) session.getAttribute("captcha");
        if (storedCaptcha != null && storedCaptcha.equals(captcha)) {
            // 验证码正确，可以进行后续登录操作
            YogaUser yogaUser = userservice.loginCode(phone);
            if (yogaUser!=null){
                session.setAttribute("yogaUserinfo1",yogaUser);
                Map<String,Object> map = new HashMap<>();
                map.put("yogaUserinfo1",yogaUser);
                return ResultVO.success("登录成功",map);
            }
            return ResultVO.success("验证码验证成功", null);
        } else {
            // 验证码错误
            return ResultVO.fail("验证码错误");
        }
    }
    @RequestMapping("loginOut")
    public ResultVO loginOut(HttpSession session){
        session.removeAttribute("loginUser");
        return ResultVO.success("已登出");
    }
    @RequestMapping("addCAPTCHA2")
    public ResultVO addCAPTCHA2(@RequestParam String phone,HttpSession session){
        if (!TimeUtils.canSendCaptcha(session, phone)) {
            return ResultVO.fail("请等待60秒后再次发送验证码");
        }
        String captcha = LoginCode.getLoginCode();
//        boolean b = SendCode.sendCode(phone, captcha);
        session.setAttribute("captcha", captcha);
        TimeUtils.updateLastSendTime(session, phone);
        System.out.println(captcha);
        return ResultVO.success("验证码已发送",null);
    }
}
